/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-02-20
*/
package com.rzico.order.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.rzico.annotation.IgnoreSwaggerParameter;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Supplier;
import lombok.Data;

import java.math.BigDecimal;
import java.util.*;
import javax.persistence.*;
import io.swagger.annotations.ApiModelProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModelProperty;

/**
 * <pre>
 *   出入库单数据库对象
 * </pre>
 * @author Rzico Boot
 * @version 1.0
 */
@Data
@Table(name = "wx_warehouse")
@JsonIgnoreProperties(value = {"handler"})
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Warehouse {

    @Id
    @ApiModelProperty(value = "" )
    protected Long id;

    @ApiModelProperty(value = "创建日期" )
    protected java.util.Date createDate;

    @ApiModelProperty(value = "修改日期" )
    protected java.util.Date modifyDate;

    @ApiModelProperty(value = "单号" )
    protected String sn;

    @ApiModelProperty(value = "收货地址" )
    protected String address;

    @ApiModelProperty(value = "所在地" )
    protected Integer areaId;

    @ApiModelProperty(value = "所属企业")
    protected Long companyId;

    @ApiModelProperty(value = "所在地" )
    protected String areaName;

    @ApiModelProperty(value = "收货人" )
    protected String consignee;

    @ApiModelProperty(value = "联系电话" )
    protected String phone;

    @ApiModelProperty(value = "备注" )
    protected String memo;

    @ApiModelProperty(value = "单据类型(1.采购入库,2.采购退货,3.调拨入库,4.销售出库,5.销售退货,6.调拨出库,7.盘点损益,8.其他出库)" )
    protected Integer warehouseType;

    @ApiModelProperty(value = "配送方式(0.普通快递,1.到店核销,2.同城物流)")
    protected Integer shippingMethod;

    @ApiModelProperty(value = "订货方式(0.线上订单，1.人工报单，2.门店销售)")
    protected Integer orderMethod;

    @ApiModelProperty(value = "单据状态(0.未确认,1.已确认,2.已完成,3.已取消)" )
    protected Integer status;

    @ApiModelProperty(value = "供应商" )
    protected Long supplierId;

    @ApiModelProperty(value = "会员(客户)" )
    protected Long memberId;

    @ApiModelProperty(value = "来源单据" )
    protected Long orderId;

    @ApiModelProperty(value = "来源订单号" )
    protected String orderSn;

    @ApiModelProperty(value = "申请单" )
    protected Long askforId;

    @ApiModelProperty(value = "配送计划" )
    protected Long orderPlanId;

    @ApiModelProperty(value = "所属企业" )
    protected Long enterpriseId;

    @ApiModelProperty(value = "所在仓库" )
    protected Long shopId;

    @ApiModelProperty(value = "业务日期")
    protected java.util.Date orderDate;

    @ApiModelProperty(value = "订单确认时间" )
    protected java.util.Date confirmedDate;

    @ApiModelProperty(value = "实际撤消时间" )
    protected java.util.Date canceledDate;

    @ApiModelProperty(value = "实际完成时间" )
    protected java.util.Date completeDate;

    @ApiModelProperty(value = "商品合计金额" )
    protected java.math.BigDecimal subPrice;

    @ApiModelProperty(value = "商品合计数量" )
    protected BigDecimal subQuantity;

    @ApiModelProperty(value = "打印次数" )
    protected Integer sPrint;

    @ApiModelProperty(value = "开票类型:0.不开票,1.增票,2.普票" )
    protected Integer taxType;

    @JsonIgnore
    @ApiModelProperty(value = "是否删除" ,hidden = true )
    protected Boolean deleted;

    @ApiModelProperty(value = "仓管员" )
    protected String operatorId;

    @ApiModelProperty(value = "所属部门" )
    protected String officeId;

    @JsonIgnore
    @ApiModelProperty(value = "创建者" ,hidden = true )
    protected String createBy;

    @JsonIgnore
    @ApiModelProperty(value = "更新者" ,hidden = true )
    protected String modifyBy;

    @ApiModelProperty(value = "出入库明细")
    protected List<WarehouseItem> itemList = Collections.emptyList();


    public List<WarehouseItem> sortItemList() {
        List<WarehouseItem> data = new ArrayList<>();
        data.addAll(getItemList());
        Collections.sort(data, new Comparator<WarehouseItem>() {
            public int compare(WarehouseItem n1, WarehouseItem n2) {
                return n1.getProductId().compareTo(n2.getProductId());
            }
        });
        return data;
    }

    /**
     *  客户
     */
    @JsonIgnore
    @IgnoreSwaggerParameter
    protected Member member;

    /**
     *  供应商
     */
    @JsonIgnore
    @IgnoreSwaggerParameter
    protected Supplier supplier;

    public String getMemberName() {
        Member member = getMember();
        if (member!=null) {
            return member.getName();
        } else {
            return null;
        }
    }

    public void setMemberName(String value) {

    }

    public String getMemberMobile() {
        Member member = getMember();
        if (member!=null) {
            return member.getMobile();
        } else {
            return null;
        }
    }

    public void setMemberMobile(String value) {

    }

    public String getSupplierName() {
        Supplier supplier = getSupplier();
        if (supplier!=null) {
            return supplier.getName();
        } else {
            return null;
        }
    }

    public void setSupplierName(String value) {

    }

    public String getSupplierMobile() {
        Supplier supplier = getSupplier();
        if (supplier!=null) {
            return supplier.getMobile();
        } else {
            return null;
        }
    }

    public void setSupplierMobile(String value) {

    }


    public BigDecimal calculateSubQuantity() {
        BigDecimal subQuantity = BigDecimal.ZERO;
        for (WarehouseItem warehouseItem:getItemList()) {
            subQuantity = subQuantity.add(warehouseItem.getQuantity());
        }
        return subQuantity;
    }


    public BigDecimal calculateSubPrice() {
        BigDecimal subPrice = BigDecimal.ZERO;
        for (WarehouseItem warehouseItem:getItemList()) {
            subPrice = subPrice.add(warehouseItem.getAmount());
        }
        return subPrice;
    }


    public BigDecimal calculateAmountPayable() {
        BigDecimal amountPayable = BigDecimal.ZERO;
        for (WarehouseItem warehouseItem:getItemList()) {
            amountPayable = amountPayable.add(warehouseItem.getSubTotal());
        }
        return amountPayable;
    }

    public BigDecimal calculateDiscount() {
        BigDecimal discount = BigDecimal.ZERO;
        for (WarehouseItem warehouseItem:getItemList()) {
            discount = discount.add(warehouseItem.getDiscount());
        }
        return discount;
    }

}
